package com.cskaoyan.wordmemorize.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.converter.VocChapterConverter;
import com.cskaoyan.wordmemorize.dao.entity.VocBookDO;
import com.cskaoyan.wordmemorize.dao.entity.VocChapterDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocBookMapper;
import com.cskaoyan.wordmemorize.dao.mapper.VocChapterMapper;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocChapterDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.VocChapterPageRequest;
import com.cskaoyan.wordmemorize.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 顾海龙
 * @version 1.0
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<VocChapterMapper, VocChapterDO> implements ChapterService {
    @Autowired
    VocChapterMapper vocChapterMapper;

    @Autowired
    VocChapterConverter vocChapterConverter;
    @Autowired
    VocBookMapper vocBookMapper;

    //添加章节
    @Override
    public void AddChapter(VocChapterDO chapterDO) throws BusinessException {
        this.save(chapterDO);
        LambdaUpdateWrapper<VocBookDO> vocBookDOLambdaUpdateWrapper = new LambdaUpdateWrapper<VocBookDO>()
                .eq(VocBookDO::getId, chapterDO.getBookId())
                .setSql("chapter_count = chapter_count + 1");
        int update = vocBookMapper.update(vocBookDOLambdaUpdateWrapper);
    }

    //修改章节
    @Override
    public void UpdateChapter(VocChapterDO vocChapterDO) throws BusinessException {
        UpdateWrapper<VocChapterDO> vocChapterDOUpdateWrapper = new UpdateWrapper<>();
        vocChapterDOUpdateWrapper.eq("id", vocChapterDO.getId());
        this.update(vocChapterDO, vocChapterDOUpdateWrapper);
    }

    //分页查询章节列表
    @Override
    public PageDTO<VocChapterDTO> selectChapterList(VocChapterPageRequest pageRequest) throws BusinessException {
        PageDTO<VocChapterDTO> vocChapterDTOPageDTO = null;
        System.out.println(pageRequest.getPageNum());
        System.out.println(pageRequest.getCount());
        QueryWrapper<VocChapterDO> vocChapterDOQueryWrapper = new QueryWrapper<>();
        if (pageRequest.getKeyword() != null && !("".equals(pageRequest.getKeyword()))) {
            vocChapterDOQueryWrapper.like("book_name", pageRequest.getKeyword());
            System.out.println("执行第一个if");
        }
        System.out.println(pageRequest.getBookId());
        if (pageRequest.getBookId() != null && !("".equals(pageRequest.getBookId()))) {
            vocChapterDOQueryWrapper.eq("book_id", pageRequest.getBookId());
            System.out.println("执行第二个if");
        }
        if (pageRequest.getPageNum() != null && pageRequest.getPageNum() > 0){
            Page<VocChapterDO> page = new Page<>(pageRequest.getPageNum(), pageRequest.getCount());
            Page<VocChapterDO> vocChapterDOPage = vocChapterMapper.selectPage(page, vocChapterDOQueryWrapper);
            List<VocChapterDO> records = vocChapterDOPage.getRecords();
            Long total = vocChapterDOPage.getTotal();
            vocChapterDTOPageDTO = vocChapterConverter.vocChapterDOs2Page(records, total);
        }
        else {
            List<VocChapterDO> vocChapterDOS = vocChapterMapper.selectList(vocChapterDOQueryWrapper);
            vocChapterDTOPageDTO = vocChapterConverter.vocChapterDOs2Page(vocChapterDOS, (long) vocChapterDOS.size());
        }
        return vocChapterDTOPageDTO;
    }

    //批量删除章节
    @Override
    public void deleteChapterList(BatchDeleteCommand command) throws BusinessException {
        //待异常处理
        command.getIds().forEach(id->{
            deleteChapter(Long.valueOf(id));
        });
//        this.removeByIds(command.getIds());
    }

    //删除指定章节
    @Override
    public void deleteChapter(Long id) throws BusinessException {
        VocChapterDO byId = getById(id);
        this.removeById(id);
        LambdaUpdateWrapper<VocBookDO> vocBookDOLambdaUpdateWrapper = new LambdaUpdateWrapper<VocBookDO>()
                .eq(VocBookDO::getId, byId.getBookId())
                .setSql("chapter_count = chapter_count - 1");
        int update = vocBookMapper.update(vocBookDOLambdaUpdateWrapper);
    }
}
